(define-module (mach types)
  #:use-module (mach ffi)
  #:use-module (ice-9 format))

;;
;; In this file we put foreign types that are so
;; simple that they don't deserve a single file.
;;

;; A port name.
;;
;; In practice, this often refers to a port within the
;; local task @code{(task-self)}, but in principle it could refer
;; to a port within some other task (e.g., when using
;; @code{port-extract-right!} or @code{port-insert-right!}). Context
;; is required!
;;
;; This seems to be the same situation as with *nix file descriptors.
(define-public ffi:mach-port ffi:unsigned-int)
(define-public %wrap-mach-port identity)
(define-public %unwrap-mach-port identity)

(define-syntax-rule (define-port-type ffi:t %wrap-t %unwrap-t)
  (begin
    (define-public ffi:t ffi:mach-port)
    (define-public %wrap-t %wrap-mach-port)
    (define-public %unwrap-t %unwrap-mach-port)))

(define-port-type ffi:task %wrap-task %unwrap-task)
(define-port-type ffi:ipc-space %wrap-ipc-space %unwrap-ipc-space)
(define-port-type ffi:vm-task %wrap-vm-task %unwrap-vm-task)

#;(defctype port-mscount :unsigned-int "mach_port_msgcount_t type")
#;
(defctype msg-seqno :unsigned-int "mach_msg_seqno_t type")
#;
(defctype port-delta :int "mach_port_delta_t type")
#;
(defctype msg-type-number :unsigned-int "mach_msg_type_number_t type")

;; For addresses that might be outside our own address space.
;; If we know we're working in our own address space, use ffi:pointer
;; instead.
(define-public vm-address ffi:unsigned-int)
;; Length of a virtual memory region.
(define-public vm-size ffi:unsigned-int)
(define-public vm-offset ffi:unsigned-int)

#;
(defctype msg-timeout :unsigned-int "mach_msg_timeout_t type")
#;
(defctype port-urefs :unsigned-int "mach_port_urefs_t type")
#;
(defctype port-msgcount :unsigned-int "mach_port_msgcount_t type")
#;
(defctype port-rights :unsigned-int "mach_port_rights_t type")

;; XXX what's the difference with ffi:msg-seqno?
(define-public ffi:port-seqno ffi:unsigned-int)

;; Message size.
(define-public ffi:msg-size ffi:unsigned-int)
;; Message bits. (XXX where are they defined?)
(define-public ffi:msg-bits ffi:unsigned-int)
;; Message ID. (XXX what does this mean exactly?)
(define-public ffi:msg-id ffi:int)

